/*
    用于学习的demo
    把输入的语句转为lua执行
*/
#include <stdio.h>
#include <string.h>
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"

#include <time.h>
#include <sys/time.h>
#include "lua_test.h"
#include "algorithm.h"
#include "data_struct/test.h"

//开启一个lua 虚拟机
void lua_star()
{
    printf("lua mv start: \n");
    char buff[256];
    int error;
    lua_State *L = luaL_newstate();
    luaL_openlibs(L);//打开lua标准库 math, string, os
    lua_test(L);
    while (fgets(buff, sizeof(buff), stdin) != NULL){
        error = luaL_loadbuffer(L, buff, strlen(buff), "line") ||
                lua_pcall(L, 0, 0, 0);
        if (error) {
            fprintf(stderr, "%s", lua_tostring(L, -1));
            lua_pop(L, 1);
        }
    }
    lua_close(L);
}

void get_time_str(char* name)
{
	time_t timep;
	struct tm *p;
	time(&timep);
	p = gmtime(&timep);
	clock_t t = clock();
	int ms = t * 1000/ CLOCKS_PER_SEC % 1000;
	sprintf(name, "%d-%d-%d_%02d:%02d:%02d-%03d", 1900 + p->tm_year, 1 + p->tm_mon, p->tm_mday, 8 + p->tm_hour, p->tm_min, p->tm_sec, ms);
}

long getCurrentTime(){
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec * 1000 + tv.tv_usec /1000;
}

int main(int argc, char* argv[])
{
    printf("argc:%d \n", argc);
    if(argc > 1){
        printf("argv:%s \n", argv[1]);
        if(strcmp(argv[1], "lua") == 0) {
            lua_star();
            return 0;
        }
    }

char str[100];
get_time_str(str);
printf("time:%s \n", str);
long num = getCurrentTime();
printf("num:%ld \n", num);
    //sl_test();

    return 0;
}
